package top.lixunda.books3.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.books3.model.SmsInf;
import top.lixunda.books3.model.SmsResult;
import top.lixunda.books3.service.VerifyService;
import top.lixunda.books3.util.Log;
import top.lixunda.books3.util.SmsUtil;
import top.lixunda.books3.util.StringUtils;
import top.lixunda.books3.util.VerifyCode;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service(value = "verifyServiceImpl")
public class VerifyServiceImpl implements VerifyService {

    @Autowired
    private StringUtils stringUtils;

    @Autowired
    private SmsUtil smsUtil;

    @Autowired
    private VerifyCode verifyCode;
    @Autowired
    private HttpSession session;

    @Override
    public VerifyCode.VerifyInfo createLoginVerifyImage() {
        VerifyCode.VerifyInfo verifyInfo = verifyCode.getImage();
        session.setAttribute("loginImage", verifyInfo.getText());
        session.setAttribute("loginImageTime", new Date().getTime());
        Log.d("create login verify == "+verifyInfo.getText());
        return verifyInfo;
    }

    @Override
    public VerifyCode.VerifyInfo createRegisterVerifyImage() {
        VerifyCode.VerifyInfo verifyInfo = verifyCode.getImage();
        Log.d("向session中插入  registerImage---->"+verifyInfo.getText());
        session.setAttribute("registerImage", verifyInfo.getText());
        session.setAttribute("registerImageTime", new Date().getTime());
        Log.d("create register verify == "+verifyInfo.getText());
        return verifyInfo;
    }

    @Override
    public SmsResult sendRegisterSms(String phone) {
        //1. 组装短信信息
        SmsInf inf = new SmsInf().setPhoneNumber(phone).setVerifyCode(stringUtils.randomFigure(6)).setData(new Date());
        //2. 发送短信
        SmsResult result = smsUtil.sendSms(inf.getVerifyCode(), phone, SmsUtil.REGISTER);
        //3. 发送短信成功,保存验证码信息
        session.setAttribute("registerSmsInf", inf);
        return result;
    }

    @Override
    public boolean judgeLoginVerify(String verify) {
        verify = verify.toLowerCase();
        String trueVerify = (String)session.getAttribute("loginImage");
        Log.d("true verify == "+trueVerify);
        Log.d("commit verify == "+verify);
        trueVerify = (trueVerify == null ? "" : trueVerify.toLowerCase());
        return trueVerify.equals(verify);
    }

    @Override
    public boolean judgeRegisterVerify(String verify) {
        verify = verify.toLowerCase();
        String trueVerify = (String)session.getAttribute("registerImage");
        Log.d("true register verify == "+trueVerify);
        Log.d("commit register verify == "+verify);
        trueVerify = (trueVerify == null ? "" : trueVerify.toLowerCase());
        return trueVerify.equals(verify);
    }

    @Override
    public boolean judgeRegisterSms(String phone, String verify) {
        SmsInf inf = (SmsInf) session.getAttribute("registerSmsInf");
        return !(inf == null || !inf.getVerifyCode().equals(verify) || !inf.getPhoneNumber().equals(phone) ||
                new Date().getTime() - inf.getData().getTime() > 300000);
    }
}
